//
//  MeTableViewCell.m
//  WildFireChat
//
//  Created by WF Chat on 2018/10/2.
//  Copyright © 2018 WildFireChat. All rights reserved.
//

#import "WFCMeTableViewHeaderViewCell.h"
#import <SDWebImage/SDWebImage.h>
#import "UIFont+YH.h"
#import "UIColor+YH.h"
#import <WFChatUIKit/WFChatUIKit.h>
#import "YGExtendChain.h"
#import "SDAutoLayout.h"

@interface WFCMeTableViewHeaderViewCell ()
@property (strong, nonatomic) UILabel *displayName;
@property (strong, nonatomic) UILabel *userName;
@end

@implementation WFCMeTableViewHeaderViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
 
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
 
      
        
        UIImageView *arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_edit"]];
        [self.contentView addSubview:arrow];
        arrow.sd_layout.rightSpaceToView(self.contentView, 22).bottomSpaceToView(self.contentView, 60).widthIs(20).heightIs(20);

        
        self.contentView.layer.masksToBounds = YES;
    }
    return self;
}
- (UIImageView *)portrait {
    if (!_portrait) {
        _portrait = [[UIImageView alloc] initWithFrame:CGRectMake(16, 64 + 66, 82, 82)];
        _portrait.layer.cornerRadius = 41 ;
        _portrait.layer.masksToBounds = YES;
        [self.contentView addSubview:_portrait];
        _portrait.layer.borderWidth = 2;
        _portrait.layer.borderColor = [UIColor whiteColor].CGColor;
        
        
        UIImageView *me_icon_edit = [[UIImageView alloc] initWithFrame:CGRectMake(16, 64 + 66, 82, 82)];
        [self.contentView addSubview:me_icon_edit];
        me_icon_edit.image = [UIImage imageNamed:@"me_icon_edit"];
        me_icon_edit.frame = CGRectMake(82 - 22 + 16, 82- 22 + 64 + 66, 22, 22);
        
      
    }
    return _portrait;
}

- (UILabel *)displayName {
    if (!_displayName) {
        _displayName = [[UILabel alloc] initWithFrame:CGRectMake(10 + 82 + 20, 74 + 66, [UIScreen mainScreen].bounds.size.width - 64, 32)];
        [_displayName setFont:[UIFont pingFangSCWithWeight:FontWeightStyleSemibold size:24]];

        [self.contentView addSubview:_displayName];
    }
    return _displayName;
}

- (UILabel *)userName {
    if (!_userName) {
        _userName = [[UILabel alloc] initWithFrame:CGRectMake(10 + 82 + 20, 74 + 32 + 8 + 66, [UIScreen mainScreen].bounds.size.width - 128, 14)];
        [_userName setFont:[UIFont systemFontOfSize:12]];
        _userName.textColor = [UIColor colorWithHexString:@"#8D8B9B"];

        [self.contentView addSubview:_userName];
    }
    return _userName;
}

- (void)setUserInfo:(WFCCUserInfo *)userInfo {
    _userInfo = userInfo;
    
    
    [self.portrait sd_setImageWithURL:[NSURL URLWithString:[self.userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage: [WFCUImage imageNamed:@"PersonalChat"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        NSLog(@"%@ err%@",image,error);
    }];
    self.displayName.text = self.userInfo.displayName;
    
    _userName.textColor = [UIColor colorWithHexString:@"#8D8B9B"];
    
    
    NSString *str = [NSString stringWithFormat:@"TL畅聊ID:%@", self.userInfo.name];
    if (self.userInfo.extra.length > 1) {
        
        NSDictionary *dic =  [self.userInfo.extra jk_JSONStringWithDictionary];
 
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str attributes:@{}];
        [att addAttribute:NSForegroundColorAttributeName value:[[dic handleNullObjectForKey:@"isVip"] intValue] == 1 ? [UIColor colorWithHexString:@"#FFA332"] : [UIColor colorWithHexString:@"#8D8B9B"] range:NSMakeRange(str.length - self.userInfo.name.length ,   self.userInfo.name.length )];

        self.userName.attributedText = att ; ///[NSString stringWithFormat:@"TL畅聊号:%@", self.userInfo.name];
        ///
    } else {
        self.userName.text = str;
    }
    
   
    _displayName.textColor = [UIColor blackColor];

}
@end
